package com.jp.knowledge.my.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.my.adapter.MyFranchiseAdapter;
import com.jp.knowledge.my.adapter.MyGradAdapter;
import com.jp.knowledge.my.business.MyVipBusiness;
import com.jp.knowledge.my.model.ActiveDataModel;
import com.jp.knowledge.my.model.OptionSelectModel;
import com.jp.knowledge.view.RecycleViewDiver;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员级别
 *
 * @author gzejia 978862664@qq.com
 */
public class MyVipActivity extends SlidingActivity {

    @ViewInject(R.id.my_rv_grad)
    private RecyclerView mGradRv;

    @ViewInject(R.id.my_tv_grad)
    private TextView mGradTv;

    @ViewInject(R.id.my_tv_integration)
    private TextView mIntegrationTv;

    @ViewInject(R.id.my_rv_franchise)
    private RecyclerView mFranchiseRv;

    @ViewInject(R.id.my_tv_franchise_organize)
    private TextView mFranchiseOrganizeTv;

    @ViewInject(R.id.my_rv_franchise_organize)
    private RecyclerView mFranchiseOrganizeRv;

    private MyVipBusiness mBusiness = new MyVipBusiness();

    @Override
    protected int getResourceId() {
        return R.layout.activity_my_vip;
    }

    @Override
    protected void init() {
        initTitle();
        initOthers();
        initGradRv(mBusiness.getGradItems(235));// 假定当前会员积分235
        initFranchisePersonalRv(mBusiness.getFranchisePersonalData(getFranchiseIndex(
                MyVipBusiness.FRANCHISE_PERSONAL_SIZE)));
        initFranchiseOrganizeRv(mBusiness.getFranchiseOrganizeData(getFranchiseIndex(
                MyVipBusiness.FRANCHISE_ORGANIZE_SIZE)));
    }

    @Event(value = {R.id.icon_left, R.id.my_lly_integration_history, R.id.my_lly_integration_shop},
            type = View.OnClickListener.class)
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_lly_integration_history:// 积分记录
                break;
            case R.id.my_lly_integration_shop:// 积分商城
                break;
            case R.id.icon_left:// 返回
                finish();
                break;
        }
    }

    private void initTitle() {
        topName.setText(R.string.my_tv_vip);
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
    }

    private void initGradRv(List<OptionSelectModel> list) {
        mGradRv.setHasFixedSize(true);
        mGradRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        MyGradAdapter mGradAdapter = new MyGradAdapter(this, list);
        mGradRv.setAdapter(mGradAdapter);
    }

    private void initFranchisePersonalRv(List<ActiveDataModel> list) {
        mFranchiseRv.setHasFixedSize(true);
        mFranchiseRv.addItemDecoration(new RecycleViewDiver(this, LinearLayout.VERTICAL));
        mFranchiseRv.setLayoutManager(new GridLayoutManager(this, 4));
        MyFranchiseAdapter mFranchiseAdapter = new MyFranchiseAdapter(this, list);
        mFranchiseRv.setAdapter(mFranchiseAdapter);
    }

    private void initFranchiseOrganizeRv(List<ActiveDataModel> list) {
        mFranchiseOrganizeRv.setHasFixedSize(true);
        mFranchiseOrganizeRv.addItemDecoration(new RecycleViewDiver(this, LinearLayout.VERTICAL));
        mFranchiseOrganizeRv.setLayoutManager(new GridLayoutManager(this, 4));
        MyFranchiseAdapter mFranchiseAdapter = new MyFranchiseAdapter(this, list);
        mFranchiseOrganizeRv.setAdapter(mFranchiseAdapter);
    }

    private void initOthers() {
        mGradTv.setText("初级能人V1");
        mIntegrationTv.setText(String.valueOf(235));
        mFranchiseOrganizeTv.setText("（" + "舰级公司" + "）");
    }

    /**
     * @return 假定用户活跃数据数据量
     */
    private List<Integer> getFranchiseIndex(int maxSize) {
        List<Integer> index = new ArrayList<>();
        for (int i = 0; i < maxSize; i++) {
            index.add((int) (Math.random() * 399) - 199);
        }
        return index;
    }
}
